--#include "data\functions\Activity\OpenServer\OpenServerSports.lua" once
--#include "data\config\equip\EquipFurnaceConfig.lua" once
--#include "data\config\attr\FuncAttrsConfig.lua" once
--#include "data\functions\Activity\OpenServer\OpenServerPKRank.lua" once   

--[[神炉装备激活
		itStoveBloodRune	= 10,				//血符（神炉装备）
		itStoveShield		= 11,				//护盾（神炉装备）
		itStoveDiamond		= 12,				//武魂（神炉装备）
		itStoveSealBead		= 13,				//灵珠（神炉装备）
		itDecoration		= 5					//勋章（神炉装备）

说明
1、神炉装备有5种，初始都为0级；
2、神炉装备激活后，为1级，同时获得一件对应的装备道具，并且自动穿戴；所以激活、升级的时候背包只是至少要有一个空格；
3、神炉装备不能直接手工穿戴；
4、神炉装备升级后，原装备被删除；
]]

FurnaceEquipIdx_BloodRune 	= 1				--血符
FurnaceEquipIdx_Shield 		= 2				--神盾
FurnaceEquipIdx_Diamond 	= 3				--武魂
FurnaceEquipIdx_SealBead 	= 4				--灵珠
FurnaceEquipIdx_Decoration 	= 5				--勋章
FurnaceEquipIdx_ReliveRing 	= 6				--复活戒指
FurnaceEquipIdx_DizzyRing 	= 7				--麻痹戒指
FurnaceEquipIdx_ProtectRing	= 8				--护体戒指

--[[
激活神炉装备
协议(7,16)
]]
function EquipFurnaceActivate(sysarg, equipFurnaceIdx )
	--print("EquipFurnaceActivate, equipFurnaceIdx="..equipFurnaceIdx)
	local furnaceEquipCfg = EquipFurnaceCfg[equipFurnaceIdx]
	if not furnaceEquipCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace001, ttFlyTip)
		return
	end

	--防止外挂钻空子
	local job = Actor.getIntProperty( sysarg, PROP_ACTOR_VOCATION )
	if ( job == 2 or job == 3 ) and equipFurnaceIdx == FurnaceEquipIdx_DizzyRing then 		--法师、道士不能激活麻痹戒指
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EquipFurnace020, ttFlyTip )
		return
	elseif job == 1 and equipFurnaceIdx == FurnaceEquipIdx_ProtectRing then 				--战士不能激活护体戒指
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EquipFurnace020, ttFlyTip )
		return
	end

	local furnaceEquipPtr = GetEquipFurnaceByIdx( sysarg, equipFurnaceIdx )				--装备神炉等级
	if furnaceEquipPtr then		--已经有神炉装备
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace004, ttFlyTip)
		return
	end

	local furnaceLevel 	= 1
	local furnaceStar 	= 1
	local furnaceEquip = furnaceEquipCfg[furnaceLevel]
	if not furnaceEquip then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace002, ttFlyTip)
		return
	end

	local equipId 	= furnaceEquip.itemId
	if not equipId then
		return
	end

	if not Item.canEquipTakeOn( sysarg, equipId ) then 		--该装备不能穿上，不满足激活条件
		--Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace010, ttFlyTip)local needCircle = Item.getItemPropertyById(nextEquipId, Item.ipItemNeedCircle) or 0
		local needCircle = Item.getItemPropertyById(equipId, Item.ipItemNeedCircle) or 0
		local needLevel  = Item.getItemPropertyById(equipId, Item.ipItemActorLevel) or 0
		if needCircle <= 0 then  		--不要求转数，只要求等级
			Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipFurnace016, needLevel), ttFlyTip )
		elseif needLevel <= 1 then 		--要求转数，不要求等级
			Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipFurnace017, needCircle), ttFlyTip )
		else 							--要求转数+等级
			Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipFurnace018, needCircle, needLevel), ttFlyTip )
		end
		return
	end
	
	--先加到背包再装备上，所以要检查背包格子数
	local gridCount = Item.getBagEmptyGridCount(sysarg)
	if gridCount < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return
	end

	--激活的消耗
	local upgradeConsume = furnaceEquip.upgradeConsumes[furnaceStar]
	if not upgradeConsume then
		return
	end

	if not CheckConsumeCond( sysarg, upgradeConsume ) then
		return 
	end

	if not DoConsumeCond( sysarg, upgradeConsume, 1, GameLog.clEquipFurnaceOpFee, Lang.ScriptTips.EquipLog008 ) then
		return 
	end

	local equipPtr = Item.createItem(equipId, 1, 0, 0, 1)
	if not equipPtr then
		return
	end

	local addCount = Item.addItem( sysarg, equipPtr, Lang.ScriptTips.EquipLog008, GameLog.clEquipFurnaceAdd, 1)
	if addCount < 1 then
		return
	end

	--先进入背包或身上，才能设置属性
	Item.setItemProperty( sysarg, equipPtr, Item.ipItemFurnaceStar, 1) 

	if Item.equipItem( sysarg, equipPtr ) then
		local actorId 	= Actor.getActorId( sysarg )
		local actorName = Actor.getName( sysarg )
		local itemid 	= Item.getItemProperty( sysarg, equipPtr, Item.ipItemID, 0)
		System.sendCommonLog(GameLog.clEquipFurnaceUpgrade, actorId, actorName, equipFurnaceIdx, itemid, 1, "", "", Lang.ScriptTips.EquipLog013)

		--特戒才发公告
		if FurnaceEquipIdx_ReliveRing <= equipFurnaceIdx and  equipFurnaceIdx <= FurnaceEquipIdx_ProtectRing then
			local itemLink = Item.getItemLinkMsg( equipId, equipPtr )
			local msg = string.format( Lang.ScriptTips.EquipFurnace019, Actor.getName( sysarg ), itemLink )
			System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow )
		end
	else 		--穿上失败，删除背包中的装备
		Item.removeItem(sysarg, equipPtr, 1, "del in bag", GameLog.clEquipFurnaceDelInBag) 
	end

	
	if equipFurnaceIdx == FurnaceEquipIdx_Diamond then
		OpenServerTriggleEvent(sysarg, enOpenServerId_Wuhun, 1, 0)			--开服活动-神炉宝石
	elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
		OpenServerTriggleEvent(sysarg, enOpenServerId_SoulBall, 1, 0)			--开服活动-神炉魂珠
	end
	
	if equipFurnaceIdx == FurnaceEquipIdx_Diamond then
		OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_Wuhun, 1, 0)			--开服活动-神炉宝石
	elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
		OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_SoulBall, 1, 0)			--开服活动-神炉魂珠
	elseif FurnaceEquipIdx_ReliveRing <= equipFurnaceIdx and  equipFurnaceIdx <= FurnaceEquipIdx_ProtectRing then
		OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_SpecialRing, 1, 0)
	end
end

--[[
神炉装备升级
协议(7,16)
]]
function EquipFurnaceUpgrade(sysarg, equipFurnaceIdx )
	--print("EquipFurnaceUpgrade, equipFurnaceIdx="..equipFurnaceIdx)
	local furnaceEquipCfg = EquipFurnaceCfg[equipFurnaceIdx]
	if not furnaceEquipCfg then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace001, ttFlyTip)
		return
	end

	--防止外挂钻空子
	local job = Actor.getIntProperty( sysarg, PROP_ACTOR_VOCATION )
	if ( job == 2 or job == 3 ) and equipFurnaceIdx == FurnaceEquipIdx_DizzyRing then 		--法师、道士不能升级麻痹戒指
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EquipFurnace021, ttFlyTip )
		return
	elseif job == 1 and equipFurnaceIdx == FurnaceEquipIdx_ProtectRing then 				--战士不能升级护体戒指
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.EquipFurnace021, ttFlyTip )
		return
	end
	

	local furnaceEquipPtr = GetEquipFurnaceByIdx( sysarg, equipFurnaceIdx )				--装备神炉等级
	if not furnaceEquipPtr then		--没有神炉装备
		local equipName = GetEquipFurnaceName( equipFurnaceIdx )
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.EquipFurnace005, equipName), ttFlyTip)
		return
	end

	--[[总星级
	星阶：从0-9,	星级：从0-9
	0	:0阶-1星
	1	:0阶0星
	10  :0阶9星
	11	:1阶0星
	100 :9阶9星
	]]
	local furnaceStarAll  = Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemFurnaceStar, 0)	
	if furnaceStarAll <= 0 then
		local equipName = GetEquipFurnaceName( equipFurnaceIdx )
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.EquipFurnace005, equipName), ttFlyTip)
		return
	end

	local curFurnaceLevel,  curFurnaceStar  = Item.getCurFurnaceStar( furnaceEquipPtr, curFurnaceLevel, curFurnaceStar )
	local nextFurnaceLevel, nextFurnaceStar = Item.getNextFurnaceStar( furnaceEquipPtr, nextFurnaceLevel, nextFurnaceStar )
	curFurnaceLevel = curFurnaceLevel + 1 		--0到N-1 转换成 1到N
	curFurnaceStar  = curFurnaceStar  + 1 		--0到N-1 转换成 1到N
	--print("EquipFurnaceUpgrade, furnaceStarAll="..furnaceStarAll..", curFurnaceLevel="..curFurnaceLevel..", curFurnaceStar="..curFurnaceStar)

	--注意：nextFurnaceLevel：从0到9，nextFurnaceStar：从1到10
	nextFurnaceLevel = nextFurnaceLevel + 1 		--0到N-1 转换成 1到N
	nextFurnaceStar  = nextFurnaceStar  + 1 		--0到N-1 转换成 1到N
	local nextFurnaceStarAll = furnaceStarAll + 1
	--print("EquipFurnaceUpgrade, nextFurnaceStarAll="..nextFurnaceStarAll..", nextFurnaceLevel="..nextFurnaceLevel..", nextFurnaceStar="..nextFurnaceStar)

	--升级的消耗
	local nextFurnaceEquip = furnaceEquipCfg[nextFurnaceLevel]
	if not nextFurnaceEquip then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace002, ttFlyTip)
		return
	end

	local upgradeConsume = nextFurnaceEquip.upgradeConsumes[nextFurnaceStar]
	if not upgradeConsume then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace002, ttFlyTip)
		return
	end

	local actorId 	= Actor.getActorId( sysarg )
	local actorName = Actor.getName( sysarg )
	if nextFurnaceLevel == curFurnaceLevel then		--等级不变,只设置星级
		if not CheckConsumeCond( sysarg, upgradeConsume ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace008, ttFlyTip)
			return 
		end

		if not DoConsumeCond( sysarg, upgradeConsume, 1, GameLog.clEquipFurnaceOpFee, Lang.ScriptTips.EquipLog008 ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace008, ttFlyTip)
			return 
		end

		Item.setItemProperty( sysarg, furnaceEquipPtr, Item.ipItemFurnaceStar, nextFurnaceStarAll)	--nextFurnaceStarAll是累加的

		--记录日志
		local itemid = Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemID, 0)
		System.sendCommonLog(GameLog.clEquipFurnaceUpgrade, actorId, actorName, equipFurnaceIdx, itemid, nextFurnaceStarAll, "", "", 
			Lang.ScriptTips.EquipLog012)
	else 		--等级变了，换新装备
		local equipId 		= Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemID, 0)
		local nextEquipId 	= nextFurnaceEquip.itemId
		if not nextEquipId then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace002, ttFlyTip)
			return
		end
		--print("EquipFurnaceUpgrade, nextEquipId="..nextEquipId..", needLevel="..needLevel..", needCircle="..needCircle)

		--[[
		local needLevel 	= Item.getItemPropertyById(nextEquipId, Item.ipItemActorLevel)
		local needCircle 	= Item.getItemPropertyById(nextEquipId, Item.ipItemNeedCircle)
		--print("EquipFurnaceUpgrade, nextEquipId="..nextEquipId..", needLevel="..needLevel..", needCircle="..needCircle)
		if not Actor.checkActorLevel(sysarg, needLevel, needCircle) then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016, ttFlyTip)
			return
		end
		]]

		if not Item.canEquipTakeOn( sysarg, nextEquipId ) then 		--不满足升级条件
			local needCircle = Item.getItemPropertyById(nextEquipId, Item.ipItemNeedCircle) or 0
			local needLevel  = Item.getItemPropertyById(nextEquipId, Item.ipItemActorLevel) or 0
			if needCircle <= 0 then  		--不要求转数，只要求等级
				Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipFurnace014, needLevel), ttFlyTip )
			elseif needLevel <= 1 then 		--要求转数，不要求等级
				Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipFurnace015, needCircle), ttFlyTip )
			else 							--要求转数+等级
				Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.EquipFurnace003, needCircle, needLevel), ttFlyTip )
			end
			return
		end

		--先加到背包再装备上，所以要检查背包格子数
		local gridCount = Item.getBagEmptyGridCount(sysarg)
		if gridCount < 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
			return
		end

		if not CheckConsumeCond( sysarg, upgradeConsume ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace008, ttFlyTip)
			return 
		end

		if not DoConsumeCond( sysarg, upgradeConsume, 1, GameLog.clEquipFurnaceOpFee, Lang.ScriptTips.EquipLog008 ) then
			--Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace008, ttFlyTip)
			return 
		end

		local itemStar  	= Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemStrong, 0)			--强化星级
		local lostStar		= Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemLostStar, 0)		--损失星级
		local itemInject  	= Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemInjectLevel, 0)		--注灵等级
		--local smithCount 	= Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemForgeTimes, 0)		--洗练次数（洗练功能开放时放开）

		--删掉原装备
		if Item.removeEquip(sysarg, furnaceEquipPtr, Lang.ScriptTips.EquipLog008, GameLog.clEquipFurnaceOpFee) < 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace006, ttFlyTip)
			return
		end
		
		local nextEquipPtr = Item.createItem(nextEquipId, 1, 0, 0, 1)
		if not nextEquipPtr then
			return
		end

		--Item.transSmithAttrs( furnaceEquipPtr, nextEquipPtr )					--进行洗练属性转移，此时原装备必须存在（洗练功能开放时放开）

		--神炉装备先进入背包
		local addCount = Item.addItem( sysarg, nextEquipPtr, Lang.ScriptTips.EquipLog008, GameLog.clEquipFurnaceAdd, 1)
		if addCount < 1 then
			return
		end

		--装备进入背包后才能设置属性
		--print("EquipFurnaceUpgrade, itemStar="..itemStar..", lostStar="..lostStar..", itemInject="..itemInject)
		Item.setItemProperty( sysarg, nextEquipPtr, Item.ipItemFurnaceStar, nextFurnaceStarAll) 	--nextFurnaceStarAll是累加的
		Item.setItemProperty( sysarg, nextEquipPtr, Item.ipItemStrong, itemStar)			--设置强化星级
		Item.setItemProperty( sysarg, nextEquipPtr, Item.ipItemLostStar, lostStar)     		--设置损失星级
		Item.setItemProperty( sysarg, nextEquipPtr, Item.ipItemInjectLevel, itemInject)		--设置注灵等级
		--Item.setItemProperty( sysarg, nextEquipPtr, Item.ipItemForgeTimes, smithCount)		--洗练次数+1（洗练功能开放时放开）

		if Item.equipItem( sysarg, nextEquipPtr ) then		--穿上装备
			--记录日志
			local itemid = Item.getItemProperty( sysarg, nextEquipPtr, Item.ipItemID, 0)
			System.sendCommonLog(GameLog.clEquipFurnaceUpgrade, actorId, actorName, equipFurnaceIdx, itemid, nextFurnaceStarAll, "", "", 
				Lang.ScriptTips.EquipLog012)
		else 		--穿上失败，删除背包中的装备
			Item.removeItem(sysarg, nextEquipPtr, 1, "del in bag", GameLog.clEquipFurnaceDelInBag) 
		end
	end

	
	if equipFurnaceIdx == FurnaceEquipIdx_Diamond then
		OpenServerTriggleEvent(sysarg, enOpenServerId_Wuhun, nextFurnaceStarAll, 0)			--开服活动-神炉宝石
		OnCarnivalOpenServerRankTriggerEvent(sysarg, Carnival_Rank_Type_StoveDiamond_4, nextFurnaceStarAll)
	elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
		OpenServerTriggleEvent(sysarg, enOpenServerId_SoulBall, nextFurnaceStarAll, 0)			--开服活动-神炉魂珠
		OnCarnivalOpenServerRankTriggerEvent(sysarg, Carnival_Rank_Type_StoveSealBead_5, nextFurnaceStarAll)
	end
	

	if equipFurnaceIdx == FurnaceEquipIdx_Diamond then
		OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_Wuhun, nextFurnaceStarAll, 0)			--开服活动-神炉宝石
	elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
		OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_SoulBall, nextFurnaceStarAll, 0)			--开服活动-神炉魂珠
	elseif FurnaceEquipIdx_ReliveRing <= equipFurnaceIdx and  equipFurnaceIdx <= FurnaceEquipIdx_ProtectRing then
		OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_SpecialRing, nextFurnaceStarAll, 0)
	end

	--aAchieveEquipLevelUpgrade = 104,
	--qtEquipUpgrade = 47,
	Actor.triggerAchieveEvent(sysarg, 104, equipFurnaceIdx, nextFurnaceStarAll)
	Actor.triggerQuestEvent(sysarg, 47, equipFurnaceIdx, nextFurnaceStarAll, false)
end

function GetEquipFurnaceName( equipFurnaceIdx )
	if equipFurnaceIdx == FurnaceEquipIdx_BloodRune then
		return Lang.ScriptTips.EquipFurnaceName1
	elseif equipFurnaceIdx == FurnaceEquipIdx_Shield then
		return Lang.ScriptTips.EquipFurnaceName2
	elseif equipFurnaceIdx == FurnaceEquipIdx_Diamond then
		return Lang.ScriptTips.EquipFurnaceName3
	elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
		return Lang.ScriptTips.EquipFurnaceName4
	elseif equipFurnaceIdx == FurnaceEquipIdx_Decoration then
		return Lang.ScriptTips.EquipFurnaceName5
	elseif equipFurnaceIdx == FurnaceEquipIdx_ReliveRing then 			--复活戒指
		return Lang.ScriptTips.EquipFurnaceName6
	elseif equipFurnaceIdx == FurnaceEquipIdx_DizzyRing then 			--麻痹戒指
		return Lang.ScriptTips.EquipFurnaceName7
	elseif equipFurnaceIdx == FurnaceEquipIdx_ProtectRing then 			--护体戒指
		return Lang.ScriptTips.EquipFurnaceName8
	end
	return ""
end


--[[根据idx获取当前该神炉装备的实例和等级
equipFurnaceId定义：
	FurnaceEquipIdx_BloodRune 	= 1				--血符
	FurnaceEquipIdx_Shield 		= 2				--神盾
	FurnaceEquipIdx_Diamond 	= 3				--武魂
	FurnaceEquipIdx_SealBead 	= 4				--灵珠
	FurnaceEquipIdx_Decoration 	= 5				--勋章
	FurnaceEquipIdx_ReliveRing 	= 6				--复活戒指
	FurnaceEquipIdx_DizzyRing 	= 7				--麻痹戒指
	FurnaceEquipIdx_ProtectRing	= 8				--护体戒指
]]
function GetEquipFurnaceByIdx( sysarg, equipFurnaceIdx )
	--print("GetEquipFurnaceByIdx, equipFurnaceIdx="..equipFurnaceIdx)
	local furnaceEquipCfg = EquipFurnaceCfg[equipFurnaceIdx]
	if not furnaceEquipCfg then
		return nil
	end

	local equipPos = 0
	if equipFurnaceIdx == FurnaceEquipIdx_BloodRune then
		equipPos = itStoveBloodRunePos
	elseif equipFurnaceIdx == FurnaceEquipIdx_Shield then
		equipPos = itStoveShieldPos
	elseif equipFurnaceIdx == FurnaceEquipIdx_Diamond then
		equipPos = itStoveDiamondPos
	elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
		equipPos = itStoveSealBeadPos
	elseif equipFurnaceIdx == FurnaceEquipIdx_Decoration then
		equipPos = itStoveDecorationPos
	elseif equipFurnaceIdx == FurnaceEquipIdx_ReliveRing then 			--复活戒指，在第2个特戒位置
		equipPos = itRightSpecialRingPos
	elseif equipFurnaceIdx == FurnaceEquipIdx_DizzyRing then 			--麻痹戒指，在第1个特戒位置
		equipPos = itLeftSpecialRingPos
	elseif equipFurnaceIdx == FurnaceEquipIdx_ProtectRing then 			--护体戒指，在第1个特戒位置
		equipPos = itLeftSpecialRingPos
	else
		return nil, 0
	end
	--print("GetEquipFurnaceByIdx, equipPos="..equipPos)
	
	local equipPtr = Item.getEquipByPos(sysarg, equipPos)
	if not equipPtr then
		return nil
	end
	return equipPtr
end

function GetEquipFurnaceByType( sysarg, equipType )
	local equipFurnaceIdx = equipType - Item.itStoveBloodRune + 1
	return GetEquipFurnaceByIdx( sysarg, equipFurnaceIdx )
end

function CheckSetEquipFurnaceByIdx( sysarg, equipFurnaceIdx, newFurnaceStarAll )
	--print("CheckSetEquipFurnaceByIdx, equipFurnaceIdx="..equipFurnaceIdx..", newFurnaceStarAll="..newFurnaceStarAll)
	if GetEquipFurnacePropByIdx( equipFurnaceIdx ) <= 0 then
		return nil
	end

	local furnaceEquipCfg = EquipFurnaceCfg[equipFurnaceIdx]
	if not furnaceEquipCfg then
		return nil
	end

	local furnaceEquipPtr = GetEquipFurnaceByIdx( sysarg, equipFurnaceIdx )				--装备神炉等级
	if not furnaceEquipPtr then		--没有神炉装备
		local equipName = GetEquipFurnaceName( equipFurnaceIdx )
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.EquipFurnace005, equipName), ttFlyTip)
		return nil
	end

	local furnaceStarAll  = Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemFurnaceStar, 0)	
	if furnaceStarAll <= 0 then
		local equipName = GetEquipFurnaceName( equipFurnaceIdx )
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.EquipFurnace005, equipName), ttFlyTip)
		return nil
	end

	local nextFurnaceStarAll = furnaceStarAll + 1
	--print("CheckSetEquipFurnaceByIdx, nextFurnaceStarAll="..nextFurnaceStarAll..", nextFurnaceLevel="..nextFurnaceLevel..", nextFurnaceStar="..nextFurnaceStar)

	--判断当前等级是否已满（没有下一个等级，就是当前等级已满）
	local nextEquipId = GetEquipFurnaceEquipIdByAllStar( equipFurnaceIdx, nextFurnaceStarAll )
	--print("CheckSetEquipFurnaceByIdx, nextEquipId="..nextEquipId)
	if nextEquipId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace002, ttFlyTip)
		return nil
	end

	--判断新等级是否超过最大等级(注意，新等级相对于当前等级可能是跳跃的)
	local newEquipId = GetEquipFurnaceEquipIdByAllStar( equipFurnaceIdx, newFurnaceStarAll )
	--print("CheckSetEquipFurnaceByIdx, newEquipId="..newEquipId)
	if newEquipId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace011, ttFlyTip)
		return nil
	end

	if not Item.canEquipTakeOn( sysarg, newEquipId ) then 		--新装备不能穿上，不能设置
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.EquipFurnace012, ttFlyTip)
		return nil
	end

	return furnaceEquipPtr
end

--[[
获取星级对应的神炉装备ID
]]
function GetEquipFurnaceEquipIdByAllStar(  equipFurnaceIdx, furnaceStarAll )
	local furnaceEquipCfg = EquipFurnaceCfg[equipFurnaceIdx]
	if not furnaceEquipCfg then
		return 0
	end

	local furnaceLevel, furnaceStar = SeperateNumByMode( furnaceStarAll-1, 10 )
	furnaceLevel = furnaceLevel + 1
	furnaceStar  = furnaceStar  + 1
	local furnaceEquip = furnaceEquipCfg[furnaceLevel]
	if not furnaceEquip then
		return 0
	end

	local upgradeConsume = furnaceEquip.upgradeConsumes[furnaceStar]
	if not upgradeConsume then
		return 0
	end

	return furnaceEquip.itemId or 0
end

function SetEquipFurnaceByIdx( sysarg, equipFurnaceIdx, newFurnaceStarAll, addFurnacePropValue  )
	local furnaceEquipPtr = CheckSetEquipFurnaceByIdx( sysarg, equipFurnaceIdx, newFurnaceStarAll )   --确保已经存在装备
	if  not furnaceEquipPtr then
		return false
	end

	local furnaceStarAll  = Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemFurnaceStar, 0)	
	local curFurnaceLevel,  curFurnaceStar  = Item.getCurFurnaceStar( furnaceEquipPtr, curFurnaceLevel, curFurnaceStar )
	curFurnaceLevel = curFurnaceLevel + 1 		--0到N-1 转换成 1到N
	curFurnaceStar  = curFurnaceStar  + 1 		--0到N-1 转换成 1到N
	--print("SetEquipFurnaceByIdx, furnaceStarAll="..furnaceStarAll..", curFurnaceLevel="..curFurnaceLevel..", curFurnaceStar="..curFurnaceStar)

	local newFurnaceLevel, newFurnaceStar = SeperateNumByMode( newFurnaceStarAll-1, 10 )
	newFurnaceLevel = newFurnaceLevel + 1
	newFurnaceStar  = newFurnaceStar  + 1
	--print("SetEquipFurnaceByIdx, newFurnaceStarAll="..newFurnaceStarAll..", newFurnaceLevel="..newFurnaceLevel..", newFurnaceStar="..newFurnaceStar)

	if newFurnaceStarAll > furnaceStarAll then
		if newFurnaceLevel == curFurnaceLevel then		--等级不变,只设置星级
			Item.setItemProperty( sysarg, furnaceEquipPtr, Item.ipItemFurnaceStar, newFurnaceStarAll)	--newFurnaceStarAll是累加的
			
			--触发活动
			if equipFurnaceIdx == FurnaceEquipIdx_Diamond then
				OpenServerTriggleEvent(sysarg, enOpenServerId_Wuhun, newFurnaceStarAll, 0)			--开服活动-神炉宝石
			elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
				OpenServerTriggleEvent(sysarg, enOpenServerId_SoulBall, newFurnaceStarAll, 0)			--开服活动-神炉魂珠
			end
			
			if equipFurnaceIdx == FurnaceEquipIdx_Diamond then
				OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_Wuhun, newFurnaceStarAll, 0)			--开服活动-神炉宝石
				OnCarnivalOpenServerRankTriggerEvent(sysarg, Carnival_Rank_Type_StoveDiamond_4, newFurnaceStarAll)
			elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
				OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_SoulBall, newFurnaceStarAll, 0)			--开服活动-神炉魂珠
				OnCarnivalOpenServerRankTriggerEvent(sysarg, Carnival_Rank_Type_StoveSealBead_5, newFurnaceStarAll)
			elseif FurnaceEquipIdx_ReliveRing <= equipFurnaceIdx and  equipFurnaceIdx <= FurnaceEquipIdx_ProtectRing then
				OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_SpecialRing, newFurnaceStarAll, 0)
			end
			--aAchieveEquipLevelUpgrade = 104,
			--qtEquipUpgrade = 47,
			Actor.triggerAchieveEvent(sysarg, 104, equipFurnaceIdx, newFurnaceStarAll)
			Actor.triggerQuestEvent(sysarg, 47, equipFurnaceIdx, newFurnaceStarAll, false)
		else 		--等级变了，换新装备
			local itemStar  	= Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemStrong, 0)			--强化星级
			local lostStar		= Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemLostStar, 0)		--损失星级
			local itemInject  	= Item.getItemProperty( sysarg, furnaceEquipPtr, Item.ipItemInjectLevel, 0)		--注灵等级

			local newEquipId = GetEquipFurnaceEquipIdByAllStar( equipFurnaceIdx, newFurnaceStarAll )
			--print("SetEquipFurnaceByIdx, newEquipId="..newEquipId)
			if newEquipId <= 0 then
				return false
			end

			if not Item.canEquipTakeOn( sysarg, newEquipId ) then 		--新装备不能穿上，不能设置
				return false
			end

			--删掉原装备
			if Item.removeEquip(sysarg, furnaceEquipPtr, Lang.ScriptTips.EquipLog008, GameLog.clEquipFurnaceOpFee) < 1 then
				return false
			end
		
			--创建1件新的装备	
			local newEquipPtr = Item.createItem(newEquipId, 1, 0, 0, 1)
			if not newEquipPtr then
				return false
			end

			local addCount = Item.addItem( sysarg, newEquipPtr, Lang.ScriptTips.EquipLog008, GameLog.clEquipFurnaceAddByItem, 1)
			if addCount < 1 then
				return false
			end

			--print("SetEquipFurnaceByIdx, itemStar="..itemStar..", lostStar="..lostStar..", itemInject="..itemInject)
			Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemFurnaceStar, newFurnaceStarAll) 	--newFurnaceStarAll是累加的
			Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemStrong, itemStar)				--设置强化星级
			Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemLostStar, lostStar)     		--设置损失星级
			Item.setItemProperty( sysarg, newEquipPtr, Item.ipItemInjectLevel, itemInject)		--设置注灵等级

			if Item.equipItem( sysarg, newEquipPtr ) then
				local actorId 	= Actor.getActorId( sysarg )
				local actorName = Actor.getName( sysarg )
				local itemid 	= Item.getItemProperty( sysarg, newEquipPtr, Item.ipItemID, 0)
				System.sendCommonLog(GameLog.clEquipFurnaceSet, actorId, actorName, equipFurnaceIdx, itemid, newFurnaceStarAll, "", "", 
					Lang.ScriptTips.EquipLog014)

				local equipName = GetEquipFurnaceName( equipFurnaceIdx )
				Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.EquipFurnace013, equipName ), ttFlyTip)
			else
				Item.removeItem(sysarg, newEquipPtr, 1, "del in bag", GameLog.clEquipFurnaceDelInBag) 
			end
		
			--触发活动
			if equipFurnaceIdx == FurnaceEquipIdx_Diamond then
				OpenServerTriggleEvent(sysarg, enOpenServerId_Wuhun, newFurnaceStarAll, 0)			--开服活动-神炉宝石
				OnCarnivalOpenServerRankTriggerEvent(sysarg, Carnival_Rank_Type_StoveDiamond_4, newFurnaceStarAll)
			elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
				OpenServerTriggleEvent(sysarg, enOpenServerId_SoulBall, newFurnaceStarAll, 0)			--开服活动-神炉魂珠
			end
			

			if equipFurnaceIdx == FurnaceEquipIdx_Diamond then
				OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_Wuhun, newFurnaceStarAll, 0)			--开服活动-神炉宝石
			elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
				OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_SoulBall, newFurnaceStarAll, 0)			--开服活动-神炉魂珠
				OnCarnivalOpenServerRankTriggerEvent(sysarg, Carnival_Rank_Type_StoveSealBead_5, newFurnaceStarAll)
			elseif FurnaceEquipIdx_ReliveRing <= equipFurnaceIdx and  equipFurnaceIdx <= FurnaceEquipIdx_ProtectRing then
				OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_SpecialRing, newFurnaceStarAll, 0)
			end
			--aAchieveEquipLevelUpgrade = 104,
			--qtEquipUpgrade = 47,
			Actor.triggerAchieveEvent(sysarg, 104, equipFurnaceIdx, newFurnaceStarAll)
			Actor.triggerQuestEvent(sysarg, 47, equipFurnaceIdx, newFurnaceStarAll, false)
		end
	else
		local furancePropId = GetEquipFurnacePropByIdx( equipFurnaceIdx )
		local oldValue 		= Actor.getUIntProperty(sysarg, furancePropId)
		local newValue 		= oldValue+addFurnacePropValue
		Actor.setUIntProperty(sysarg, furancePropId, newValue)
		--print("SetEquipFurnaceByIdx, furancePropId="..furancePropId..", oldValue="..oldValue..", newValue="..newValue)
	end
	return true
end

--[[
FurnaceEquipIdx_BloodRune 	= 1				--血符
FurnaceEquipIdx_Shield 		= 2				--神盾
FurnaceEquipIdx_Diamond 	= 3				--武魂
FurnaceEquipIdx_SealBead 	= 4				--灵珠
FurnaceEquipIdx_Decoration 	= 5				--勋章
FurnaceEquipIdx_ReliveRing 	= 6				--复活戒指
FurnaceEquipIdx_DizzyRing 	= 7				--麻痹戒指
FurnaceEquipIdx_ProtectRing	= 8				--护体戒指
]]
function GetEquipFurnacePropByIdx( equipFurnaceIdx )
	if equipFurnaceIdx == FurnaceEquipIdx_BloodRune then
		return PROP_ACTOR_FURANCE_BLOODRUNE
	elseif equipFurnaceIdx == FurnaceEquipIdx_Shield then
		return PROP_ACTOR_FURANCE_SHIELD
	elseif equipFurnaceIdx == FurnaceEquipIdx_Diamond then
		return PROP_ACTOR_FURANCE_DIAMOND
	elseif equipFurnaceIdx == FurnaceEquipIdx_SealBead then
		return PROP_ACTOR_FURANCE_SEALBEAD
	elseif equipFurnaceIdx == FurnaceEquipIdx_Decoration then
		return PROP_ACTOR_ACIEVEPOINT		--勋章对应成就点
	end
	return 0
end



function GetEquipFurnaceLevelMaxByIdx( equipFurnaceIdx )
	local furnaceEquipCfg = EquipFurnaceCfg[equipFurnaceIdx]
	if not furnaceEquipCfg then
		return 0
	end
	local furnaceLevelMax = #furnaceEquipCfg or 0
	return furnaceLevelMax
end

function GetEquipFurnaceStarMax( equipFurnaceIdx )
	local furnaceEquipCfg = EquipFurnaceCfg[equipFurnaceIdx]
	if not furnaceEquipCfg then
		return 0
	end

	local levelNum 	= #furnaceEquipCfg or 0
	local starNum	= #furnaceEquipCfg[1].upgradeConsumes or 0
	return levelNum * starNum
end
